﻿using AAPL.Common;
using AAPL.Common.DataShapes;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

namespace AAPL.BAL.Persistence.Persisters
{

    public class BlogPostPersister
    {


        public void Save(BlogPost post)
        {
            if (post.PostGuid == NullValues.NullGuid)
            {
               Insert(post);
            }
            else
            {
               Update(post);
            }          
        }


        public void Insert(BlogPost newPost)
        {
            if (newPost.PostGuid == NullValues.NullGuid)
                newPost.PostGuid = Guid.NewGuid();
            SqlDao dao = new SqlDao();
            string sql = @"INSERT INTO [dbo].[BlogPost]
                               ([PostGuid]
                               ,[PostedUtc]
                               ,[BlogGuid]
                               ,[PostUrl]
                               ,[PostTitle]
                               ,[PostSummary]
                               ,[AuthorName])
                         VALUES
                               (@PostGuid
                               ,@PostedUtc
                               ,@BlogGuid
                               ,@PostUrl
                               ,@PostTitle
                               ,@PostSummary
                               ,@AuthorName)";
            SqlCommand command = dao.GetSqlCommand(sql);
            command.Parameters.Add(dao.CreateParameter("@PostGuid", newPost.PostGuid));
            command.Parameters.Add(dao.CreateParameter("@PostedUtc", newPost.PostedUtc));
            command.Parameters.Add(dao.CreateParameter("@BlogGuid", newPost.BlogGuid));
            command.Parameters.Add(dao.CreateParameter("@PostUrl", newPost.PostUrl, 200));
            command.Parameters.Add(dao.CreateParameter("@PostTitle", newPost.PostTitle, 200));
            command.Parameters.Add(dao.CreateParameter("@PostSummary", newPost.PostSummary, 2000));
            command.Parameters.Add(dao.CreateParameter("@AuthorName", newPost.PostUrl, 50));
            dao.ExecuteNonQuery(command);
        }


        public void Update(BlogPost dto)
        {
            SqlDao dao = new SqlDao();
            string sql = @"UPDATE [dbo].[BlogPost]
                           SET [PostedUtc] = @PostedUtc
                           ,[BlogGuid] = @BlogGuid
                           ,[PostUrl] = @PostUrl
                           ,[PostTitle] = @PostTitle
                           ,[PostSummary] = @PostSummary
                           ,[PostBody] = @PostBody
                           ,[AuthorName] = @AuthorName
                           ,[Score] = @Score
                           WHERE PostGuid = @PostGuid ";

            SqlCommand command = dao.GetSqlCommand(sql);
            command.Parameters.Add(dao.CreateParameter("@PostGuid", dto.PostGuid));
            command.Parameters.Add(dao.CreateParameter("@PostedUtc", dto.PostedUtc));
            command.Parameters.Add(dao.CreateParameter("@BlogGuid", dto.BlogGuid));
            command.Parameters.Add(dao.CreateParameter("@PostUrl", dto.PostUrl, 200));
            command.Parameters.Add(dao.CreateParameter("@PostTitle", dto.PostTitle, 200));
            command.Parameters.Add(dao.CreateParameter("@PostSummary", dto.PostSummary, 2000));
            command.Parameters.Add(dao.CreateParameter("@PostBody", dto.PostBody, 4000));
            command.Parameters.Add(dao.CreateParameter("@AuthorName", dto.PostUrl, 50));
            command.Parameters.Add(dao.CreateParameter("@Score", dto.Score));
            dao.ExecuteNonQuery(command);
        }


        public void Delete(BlogPost dto)
        {
            SqlDao dao = new SqlDao();
            string sql = @"DELETE FROM [dbo].[BlogPost]
                           WHERE PostGuid = @PostGuid ";

            SqlCommand command = dao.GetSqlCommand(sql);
            command.Parameters.Add(dao.CreateParameter("@PostGuid", dto.PostGuid));
            dao.ExecuteNonQuery(command);
        }

    }
}
